import 'package:flutter/material.dart';
import 'breakpoints.dart';

/// 企业级响应式工具类
/// 统一管理响应式间距、字体、尺寸等配置
class ResponsiveUtils {
  /// 获取响应式值
  static T getResponsiveValue<T>({
    required BuildContext context,
    required T mobile,
    T? tablet,
    T? desktop,
    T? largeDesktop,
  }) {
    if (AppBreakpoints.isLargeDesktop(context)) {
      return largeDesktop ?? desktop ?? tablet ?? mobile;
    }
    if (AppBreakpoints.isDesktop(context)) {
      return desktop ?? tablet ?? mobile;
    }
    if (AppBreakpoints.isTablet(context)) {
      return tablet ?? mobile;
    }
    return mobile;
  }

  /// 获取响应式间距
  static EdgeInsets getResponsivePadding(BuildContext context) {
    final padding = getResponsiveValue<double>(
      context: context,
      mobile: 16.0,
      tablet: 20.0,
      desktop: 24.0,
      largeDesktop: 32.0,
    );
    return EdgeInsets.all(padding);
  }

  /// 获取响应式水平间距
  static double getResponsiveHorizontalPadding(BuildContext context) {
    return getResponsiveValue<double>(
      context: context,
      mobile: 16.0,
      tablet: 20.0,
      desktop: 32.0,
      largeDesktop: 48.0,
    );
  }

  /// 获取响应式标题字体大小
  static double getResponsiveTitleFontSize(BuildContext context) {
    return getResponsiveValue<double>(
      context: context,
      mobile: 20.0,
      tablet: 24.0,
      desktop: 28.0,
      largeDesktop: 32.0,
    );
  }

  /// 获取响应式正文字体大小
  static double getResponsiveBodyFontSize(BuildContext context) {
    return getResponsiveValue<double>(
      context: context,
      mobile: 14.0,
      tablet: 16.0,
      desktop: 16.0,
      largeDesktop: 18.0,
    );
  }

  /// 获取响应式卡片最大宽度
  static double getResponsiveCardMaxWidth(BuildContext context) {
    return getResponsiveValue<double>(
      context: context,
      mobile: double.infinity,
      tablet: 300.0,
      desktop: 350.0,
      largeDesktop: 400.0,
    );
  }

  /// 获取响应式按钮高度
  static double getResponsiveButtonHeight(BuildContext context) {
    return getResponsiveValue<double>(
      context: context,
      mobile: 48.0,
      tablet: 52.0,
      desktop: 56.0,
      largeDesktop: 60.0,
    );
  }

  /// 获取响应式图标大小
  static double getResponsiveIconSize(BuildContext context) {
    return getResponsiveValue<double>(
      context: context,
      mobile: 20.0,
      tablet: 24.0,
      desktop: 28.0,
      largeDesktop: 32.0,
    );
  }

  /// 获取响应式圆角半径
  static double getResponsiveBorderRadius(BuildContext context) {
    return getResponsiveValue<double>(
      context: context,
      mobile: 12.0,
      tablet: 14.0,
      desktop: 16.0,
      largeDesktop: 18.0,
    );
  }

  /// 获取响应式阴影
  static List<BoxShadow> getResponsiveShadow(BuildContext context) {
    final blurRadius = getResponsiveValue<double>(
      context: context,
      mobile: 4.0,
      tablet: 6.0,
      desktop: 8.0,
      largeDesktop: 10.0,
    );
    
    return [
      BoxShadow(
        color: Colors.black.withOpacity(0.1),
        blurRadius: blurRadius,
        spreadRadius: blurRadius * 0.25,
        offset: Offset(0, blurRadius * 0.5),
      ),
    ];
  }

  /// 获取响应式网格列数
  static int getResponsiveGridColumns(BuildContext context) {
    return getResponsiveValue<int>(
      context: context,
      mobile: 1,
      tablet: 2,
      desktop: 3,
      largeDesktop: 4,
    );
  }

  /// 获取响应式最大内容宽度
  static double getResponsiveMaxContentWidth(BuildContext context) {
    return getResponsiveValue<double>(
      context: context,
      mobile: double.infinity,
      tablet: 600.0,
      desktop: 800.0,
      largeDesktop: 1200.0,
    );
  }
}

/// 响应式间距组件
class ResponsiveSizedBox extends StatelessWidget {
  final double mobile;
  final double? tablet;
  final double? desktop;
  final double? largeDesktop;
  
  const ResponsiveSizedBox({
    Key? key,
    required this.mobile,
    this.tablet,
    this.desktop,
    this.largeDesktop,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final height = ResponsiveUtils.getResponsiveValue(
      context: context,
      mobile: mobile,
      tablet: tablet ?? mobile,
      desktop: desktop ?? tablet ?? mobile,
      largeDesktop: largeDesktop ?? desktop ?? tablet ?? mobile,
    );
    
    return SizedBox(height: height);
  }
}

/// 响应式值选择器
class ResponsiveValue<T> {
  final BuildContext context;
  final T mobile;
  final T? tablet;
  final T? desktop;
  final T? largeDesktop;
  
  const ResponsiveValue({
    required this.context,
    required this.mobile,
    this.tablet,
    this.desktop,
    this.largeDesktop,
  });

  T get value => ResponsiveUtils.getResponsiveValue(
    context: context,
    mobile: mobile,
    tablet: tablet,
    desktop: desktop,
    largeDesktop: largeDesktop,
  );
}

extension ResponsiveUtilsExtension on BuildContext {
  /// 响应式间距
  EdgeInsets get responsivePadding => ResponsiveUtils.getResponsivePadding(this);
  
  /// 响应式水平间距
  double get responsiveHorizontalPadding => ResponsiveUtils.getResponsiveHorizontalPadding(this);
  
  /// 响应式标题字体大小
  double get responsiveTitleFontSize => ResponsiveUtils.getResponsiveTitleFontSize(this);
  
  /// 响应式正文字体大小
  double get responsiveBodyFontSize => ResponsiveUtils.getResponsiveBodyFontSize(this);
  
  /// 响应式卡片最大宽度
  double get responsiveCardMaxWidth => ResponsiveUtils.getResponsiveCardMaxWidth(this);
  
  /// 响应式按钮高度
  double get responsiveButtonHeight => ResponsiveUtils.getResponsiveButtonHeight(this);
  
  /// 响应式图标大小
  double get responsiveIconSize => ResponsiveUtils.getResponsiveIconSize(this);
  
  /// 响应式圆角半径
  double get responsiveBorderRadius => ResponsiveUtils.getResponsiveBorderRadius(this);
  
  /// 响应式阴影
  List<BoxShadow> get responsiveShadow => ResponsiveUtils.getResponsiveShadow(this);
  
  /// 响应式网格列数
  int get responsiveGridColumns => ResponsiveUtils.getResponsiveGridColumns(this);
  
  /// 响应式最大内容宽度
  double get responsiveMaxContentWidth => ResponsiveUtils.getResponsiveMaxContentWidth(this);
}